package com.logistics.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.logistics.compont.OrderStatusCompent;
import com.logistics.compont.QueryPageParam;
import com.logistics.compont.ResponseApi;
import com.logistics.pojo.Employee;
import com.logistics.pojo.Orderinfo;
import com.logistics.service.OrderinfoService;
import com.logistics.util.DateUtils;
import com.logistics.util.OrderExcelUtil;
import com.logistics.util.OrderUtil;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/orderinfo")
public class OrderinfoController extends BaseController{

	@Autowired
	private OrderinfoService orderinfoService;

	@GetMapping("/statisitcsOrder")
	public ResponseApi statisitcsOrder(){
		Map<String, Map<String, Integer>> result = orderinfoService.statisitcsOrder();
		return ResponseApi.SUCCESS(result);
	}

	//Excel导出
	@PostMapping("/importExcel")
	public void importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request){
		try {
			OrderExcelUtil oeu = new OrderExcelUtil();
			List<Orderinfo> orderinfoList = oeu.importCustomer(file, request);

			orderinfoService.importData(orderinfoList);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//Excel导出
	@GetMapping("/exportExcel")
	public void exportExcel(String[] onumIds){
		try {
			List<String> onums = Arrays.asList(onumIds);

			orderinfoService.exportExcel(onums);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 订单修改
	 * @param orderinfo
	 * @return
	 */
	@PostMapping("/updateOrder")
	public ResponseApi updateOrder(@RequestBody Orderinfo orderinfo){
		try {
			orderinfoService.updateOrder(orderinfo);
			return ResponseApi.SUCCESS(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseApi.FAILED(null);
		}
	}

	/**
	 * 订单修改回显
	 * @return
	 */
	@GetMapping("/findOrderByOnum")
	public ResponseApi findOrderByOnum(String onum){

		Orderinfo orderinfo = orderinfoService.findOrderByOnum(onum);
		return ResponseApi.SUCCESS(orderinfo);
	}

	/**
	 * 订单揽件
	 * @return
	 */
	@DeleteMapping("/del")
//	@RequestMapping(method=RequestMethod.DELETE,value = "/del")
	public ResponseApi del(@RequestBody Map<String,Object> paramMap){

		try {
			String onum = (String)paramMap.get("onum");
			orderinfoService.del(onum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseApi.SUCCESS("");
	}

	/**
	 * 订单揽件
	 * @return
	 */
	@GetMapping("/batchUpdateState")
	public ResponseApi batchUpdateState(@RequestBody Map<String,Object> paramMap){

		try {

			List<String> onumIds = (List<String>)paramMap.get("onum");
			String state = (String)paramMap.get("state");

//            List<String> listIds = Lists.asList(null, onumIds);
//            List<String> strings = Arrays.asList(onumIds);

//            List<String> ids = new ArrayList<String>();
//            Arrays.stream(onumIds).forEach(x -> {
//                ids.add(x);
//            });

			//list 转 string[] 参数声明转换类型
//            String[] objects = listIds.toArray(new String[0]);


			orderinfoService.batchUpdateState(onumIds,state);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseApi.SUCCESS("");
	}

	/**
	 * 订单揽件
	 * @return
	 */
	@GetMapping("/updateState")
	public ResponseApi updateState(@RequestBody Map<String,Object> paramMap){

		//map 里的属性 导入到 orderinfo
//		Orderinfo orderinfo = new Orderinfo();
//		BeanUtils.copyProperties(paramMap,orderinfo);

		try {
			orderinfoService.updateState(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseApi.SUCCESS("");
	}

	/**
	 * 返回全部列表
	 * @return
	 */
	@GetMapping("/findPage")
	public ResponseApi  findPage(@RequestBody QueryPageParam queryPageParam){
		Page page = orderinfoService.findPage(queryPageParam);
		return ResponseApi.SUCCESS(page);
	}

	/**
	 * 增加
	 * @param orderinfo
	 * @return
	 */
	@PostMapping("/addOrder")
	public ResponseApi addOrder(@RequestBody Orderinfo orderinfo){
		try {
			//获取当前登录人的信息
			Employee employee = getUser();

			orderinfo.setEid(employee.getEid());
			//订单状态
			orderinfo.setState(OrderStatusCompent.PENDING_ITEMS);
			//订单创建时间
			orderinfo.setCreatetime(DateUtils.getNow());

			//补全信息
			orderinfo.setOnum(OrderUtil.genOrderNum());
			orderinfoService.save(orderinfo);
			return ResponseApi.SUCCESS(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseApi.FAILED(null);
		}
	}

}
